package com.snack.shop.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.snack.shop.entity.User;
import com.snack.shop.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Sa-Token 权限认证接口实现
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SaTokenRoleConfig implements StpInterface {

    private final UserMapper userMapper;

    /**
     * 返回指定账号id所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 本项目暂不使用权限码，返回空列表
        return new ArrayList<>();
    }

    /**
     * 返回指定账号id所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleList = new ArrayList<>();
        
        try {
            Long userId = Long.valueOf(loginId.toString());
            User user = userMapper.selectById(userId);
            
            if (user != null) {
                // 根据 userType 返回角色
                // 1-普通用户，2-配送员，3-商家
                switch (user.getUserType()) {
                    case 1:
                        roleList.add("customer");
                        break;
                    case 2:
                        roleList.add("delivery");
                        break;
                    case 3:
                        roleList.add("merchant");
                        break;
                }
            }
            
            log.debug("用户[{}]的角色列表: {}", loginId, roleList);
        } catch (Exception e) {
            log.error("获取用户角色失败", e);
        }
        
        return roleList;
    }
}

